package com.itcam.interview.pro00.thread;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class DeadLock {
    private static class Resource {
        private final String name;

        private Resource(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    private static final Resource resource1 = new Resource("Resource1");
    private static final Resource resource2 = new Resource("Resource2");

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            synchronized (resource1) {
                System.out.println("Thread 1:locked" + resource1.getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (resource2) {
                    System.out.println("Thread 1:locked" + resource2.getName());
                }
            }
        });
        Thread thread2 = new Thread(() -> {
            synchronized (resource2) {
                System.out.println("Thread 2:locked" + resource2.getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (resource1) {
                    System.out.println("Thread 2:locked" + resource1.getName());
                    System.out.println();
                    System.out.println();
                }
            }
        });
        thread1.start();
        thread2.start();
    }
}
